﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using thinger.DataConvertLib;
using thinger.MTHModbusLib;

namespace thinger.MTHModels
{
    public class CommonMethods
    {
        //设备信息
        public static Device Device { get; set; }
        //日志委托
        public static Action<int, string> AddLog { get; set; }
        //modbus
        public static ModbusTCP ModbusTCP { get; set; } = new ModbusTCP();
        //登录后的用户信息
        public static SysAdmin sysAdmin { get; set; } = new SysAdmin();

        //通用写入
        public static bool CommonWriteModbusTcp(string varName, string varValue)
        {
            if (Device.IsConnected)
            {
                Varlable varlable = GetVarlable(Device, varName);
                if (varlable != null)
                {
                    DataType dataType = (DataType)Enum.Parse(typeof(DataType), varlable.DataType, true);

                    //线性反转换
                    string content = MigrationLib.SetMigrationValue(varValue, dataType, varlable.VarScale.ToString(), varlable.Offset.ToString()).Content;

                    try
                    {
                        switch (dataType)
                        {
                            case DataType.Bool:
                                return ModbusTCP.WriteOutputCoil(Convert.ToUInt16(varlable.Start), Convert.ToBoolean(content));
                            case DataType.Short:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromShort(Convert.ToInt16(varValue)));
                            case DataType.UShort:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromUShort(Convert.ToUInt16(varValue)));
                            case DataType.Int:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromInt(Convert.ToInt32(varValue)));
                            case DataType.UInt:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromUInt(Convert.ToUInt32(varValue)));
                            case DataType.Float:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromFloat(Convert.ToSingle(varValue)));
                            case DataType.Double:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromDouble(Convert.ToDouble(varValue)));
                            case DataType.Long:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromLong(Convert.ToInt64(varValue)));
                            case DataType.ULong:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromULong(Convert.ToUInt64(varValue)));
                            case DataType.String:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromString(varValue, Encoding.ASCII));
                            case DataType.ByteArray:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromHexString(varValue));
                            case DataType.HexString:
                                return ModbusTCP.WriteOutputRegisters(Convert.ToUInt16(varlable.Start), ByteArrayLib.GetByteArrayFromHexString(varValue));
                            default:
                                break;
                        }
                    }
                    catch (Exception)
                    {

                        CommonMethods.AddLog(2, "写入失败");
                    }

                    
                }

            }
            return false;
        }

        private static Varlable GetVarlable(Device device, string varName)
        {
            foreach (var item in device.Groups)
            {
                Varlable varlable = item.Varlables.Find(c => c.VarName == varName);
                return varlable;
            }
            return null;
        }
    }
}
